﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
namespace HRM
{
    public partial class frmCandidateList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int CandidateID = 0;
        CandidateClient wcfCandidate;
        RoleRightInfo listRole;
        bool _selected = false;
        bool _loadedData = false;
        string _Candidate_No;
        public string Candidate_No
        {
            get { return _Candidate_No; }
            set { _Candidate_No = value; }
        }
        public int Candidate_ID
        {
            get { return CandidateID; }
            set { CandidateID = value; }
        }
        public frmCandidateList(bool selected)
        {
            InitializeComponent();
            LoadLanguage();
            try
            {
                wcfCandidate = Service.Candidate();
                listRole = UIParamater.Quyen_UngVien;
                if (!listRole._create)
                    btnNew.Enabled = false;
                if (!listRole._delete)
                    btnDel.Enabled = false;
                if (!listRole.Update)
                {
                    this.btnEdit.Image = global::HRM.Properties.Resources.app;
                    this.btnEdit.Tag = "xem";
                }
                BuildVoucherGrid();
                _selected = selected;
                if (_selected)
                    UISetting.LoadRecruimentStatus1(ref cboStatusRecruiment);
                else
                    UISetting.LoadRecruimentStatus(ref cboStatusRecruiment);
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n"+ex.Message+"\n" + UpdateUI.GetString("ghiloi"));
            }
            this.KeyPreview = true;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
            this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "Candidate_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "RowNumber", "STT", "", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_No", UpdateUI.GetString("maso"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_FullName", UpdateUI.GetString("hovaten"), "", 130, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_Sex", UpdateUI.GetString("gioitinh"), "C", 90, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_DateOfBirth", UpdateUI.GetString("ngaysinh"), "C", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_PlaceOfBirth", UpdateUI.GetString("noisinh"), "", 130, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_IDCardNo", UpdateUI.GetString("CMTND"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_Address", UpdateUI.GetString("diachi"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_Phone", UpdateUI.GetString("dienthoai"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Candidate_Date", UpdateUI.GetString("ngaynophoso"), "", 120, true, true);
        }
        private void LoadData()
        {
            try
            {
                string keyWord = "";
                if (txtKeyWord.Text.Trim().Contains("'"))
                    keyWord = txtKeyWord.Text.Trim().Replace("'", "''");
                else
                    keyWord = txtKeyWord.Text.Trim();
                int _StatusID = Convert.ToInt32(cboStatusRecruiment.ComboBox.SelectedValue);
                DataSet _ds = new DataSet();
                if (_selected)
                {
                    if (_StatusID == 0)
                        //Chưa tham gia
                        _ds = wcfCandidate.Candidate_GetList(out itemCount, 0, "", keyWord, currPage, pageSize);
                    else if (_StatusID == 2)
                        //Tham gia nhưng Trượt
                        _ds = wcfCandidate.Candidate_GetList(out itemCount, _StatusID, "Truot", keyWord, currPage, pageSize);
                }
                else
                {
                    if (_StatusID == 0)
                        //Chưa tham gia
                        _ds = wcfCandidate.Candidate_GetList(out itemCount, 0, "", keyWord, currPage, pageSize);
                    else if (_StatusID == 2)
                        //Tham gia nhưng Trượt
                        _ds = wcfCandidate.Candidate_GetList(out itemCount, 2, "Truot", keyWord, currPage, pageSize);
                    else
                        //Tham gia nói chung
                        _ds = wcfCandidate.Candidate_GetList(out itemCount, 1, "", keyWord, currPage, pageSize);
                }

                PageIndex();
                dgvList.Rows.Clear();
                for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
                {
                    dgvList.Rows.Insert(i, 1);
                    dgvList.Rows[i].Cells["Candidate_ID"].Value = _ds.Tables[0].Rows[i]["Candidate_ID"].ToString();
                    dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                    dgvList.Rows[i].Cells["Candidate_No"].Value = _ds.Tables[0].Rows[i]["Candidate_No"].ToString();
                    dgvList.Rows[i].Cells["Candidate_FullName"].Value = _ds.Tables[0].Rows[i]["Candidate_LastName"].ToString()
                        + " " + _ds.Tables[0].Rows[i]["Candidate_FirstName"].ToString();
                    dgvList.Rows[i].Cells["Candidate_Sex"].Value = _ds.Tables[0].Rows[i]["Candidate_Sex"].ToString();
                    dgvList.Rows[i].Cells["Candidate_DateOfBirth"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Candidate_DateOfBirth"]).ToString("dd/MM/yyyy");
                    dgvList.Rows[i].Cells["Candidate_PlaceOfBirth"].Value = _ds.Tables[0].Rows[i]["Candidate_PlaceOfBirth"].ToString();
                    dgvList.Rows[i].Cells["Candidate_IDCardNo"].Value = _ds.Tables[0].Rows[i]["Candidate_IDCardNo"].ToString();
                    dgvList.Rows[i].Cells["Candidate_Address"].Value = _ds.Tables[0].Rows[i]["Candidate_Address"].ToString();
                    dgvList.Rows[i].Cells["Candidate_Phone"].Value = _ds.Tables[0].Rows[i]["Candidate_Phone"].ToString();
                    if (_ds.Tables[0].Rows[i]["Candidate_Date"] != DBNull.Value)
                        dgvList.Rows[i].Cells["Candidate_Date"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Candidate_Date"]).ToString("dd/MM/yyyy");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n"+ex.Message+"\n" + UpdateUI.GetString("ghiloi"));
            }
            _loadedData = true;
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmCandidate(0, listRole, 0);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                CandidateID = Convert.ToInt32(dgvList.CurrentRow.Cells["Candidate_ID"].Value);
                if (wcfCandidate.Candidate_Using(CandidateID) != 0)
                {
                    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show( UpdateUI.GetString("bancochacchanxoa"),  UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfCandidate.Candidate_Delete(CandidateID))
                    {
                        Program.taskbarNotifier.Show( UIParamater.TitleProject,  UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    CandidateID = Convert.ToInt32(dgvList.CurrentRow.Cells["Candidate_ID"].Value);
                    Form f = new frmCandidate(CandidateID, listRole, Convert.ToInt32(cboStatusRecruiment.ComboBox.SelectedValue));
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (dgvList.SelectedRows.Count > 0)
                {
                    if (_selected)
                    {
                        Candidate_ID = Convert.ToInt32(dgvList.SelectedRows[0].Cells["Candidate_ID"].Value);
                        Candidate_No = Convert.ToString(dgvList.SelectedRows[0].Cells["Candidate_No"].Value);
                        this.Close();
                    }
                    else
                        btnEdit_Click(null, null);
                }
            }
        }

        private void frmCandidateList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    if(btnEdit.Enabled)
                    btnEdit_Click(null, null);
                    break;
            }
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void cboStatusRecruiment_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                int _status = Convert.ToInt32(cboStatusRecruiment.ComboBox.SelectedValue);
                if (_status == 1)
                {
                    btnDel.Enabled = false;
                    btnEdit.Tag = "xem";
                    this.btnEdit.Image = global::HRM.Properties.Resources.app;
                }
                else
                {
                    btnDel.Enabled = true;
                    this.btnEdit.Image = global::HRM.Properties.Resources.edit;
                    btnEdit.Tag = "sua";
                }
                LoadLanguage();
                currPage = 0;
                LoadData();
            }
        }

        private void frmCandidateList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            dgvList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
            dgvList.Columns["Candidate_No"].HeaderText = UpdateUI.GetString("maso");
            dgvList.Columns["Candidate_FullName"].HeaderText = UpdateUI.GetString("hovaten");
            dgvList.Columns["Candidate_DateOfBirth"].HeaderText = UpdateUI.GetString("ngaysinh");
            dgvList.Columns["Candidate_PlaceOfBirth"].HeaderText = UpdateUI.GetString("noisinh");
            dgvList.Columns["Candidate_Address"].HeaderText = UpdateUI.GetString("diachi");
            dgvList.Columns["Candidate_Phone"].HeaderText = UpdateUI.GetString("dienthoai");
            dgvList.Columns["Candidate_Sex"].HeaderText = UpdateUI.GetString("gioitinh");
            dgvList.Columns["Candidate_IDCardNo"].HeaderText = UpdateUI.GetString("CMTND");
            dgvList.Columns["Candidate_Date"].HeaderText = UpdateUI.GetString("ngaynophoso");
            
        }

        private void frmCandidateList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfCandidate.Close();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {

            string keyWord = "";
            if (txtKeyWord.Text.Trim().Contains("'"))
                keyWord = txtKeyWord.Text.Trim().Replace("'", "''");
            else
                keyWord = txtKeyWord.Text.Trim();
            int _StatusID = Convert.ToInt32(cboStatusRecruiment.ComboBox.SelectedValue);
            DataSet ds = new DataSet();
            string _fileName = "";
            _fileName = "tmpUngVien.xls";
            try
            {
                if (_selected)
                {
                    if (_StatusID == 0)
                        //Chưa tham gia
                        ds = wcfCandidate.Candidate_GetList(out itemCount, 0, "", keyWord, currPage, pageSize);
                    else if (_StatusID == 2)
                        //Tham gia nhưng Trượt
                        ds = wcfCandidate.Candidate_GetList(out itemCount, _StatusID, "Truot", keyWord, currPage, pageSize);
                }
                else
                {
                    if (_StatusID == 0)
                        //Chưa tham gia
                        ds = wcfCandidate.Candidate_GetList(out itemCount, 0, "", keyWord, currPage, pageSize);
                    else if (_StatusID == 2)
                        //Tham gia nhưng Trượt
                        ds = wcfCandidate.Candidate_GetList(out itemCount, 2, "Truot", keyWord, currPage, pageSize);
                    else
                        //Tham gia nói chung
                        ds = wcfCandidate.Candidate_GetList(out itemCount, 1, "", keyWord, currPage, pageSize);
                }
               
            
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Remove("Candidate_ID");
                    ds.Tables[0].Columns["Rownumber"].Caption = "STT";
                    ds.Tables[0].Columns["Candidate_No"].Caption = "Mã số";
                    ds.Tables[0].Columns["CANDIDATE_FIRSTNAME"].Caption = "Tên";
                    ds.Tables[0].Columns["CANDIDATE_LASTNAME"].Caption = "Họ đệm";
                    ds.Tables[0].Columns["Candidate_FullName"].Caption = "Họ và tên";
                    ds.Tables[0].Columns["Candidate_Alias"].Caption = "Tên khác";
                    ds.Tables[0].Columns["Candidate_Sex"].Caption = "Giới tính";
                    ds.Tables[0].Columns["Ethnic_Name"].Caption = "Dân tộc";
                    ds.Tables[0].Columns["Religion_Name"].Caption = "Tôn giáo";
                    ds.Tables[0].Columns["Candidate_DateOfBirth"].Caption = "Ngày sinh";
                    ds.Tables[0].Columns["Candidate_PlaceOfBirth"].Caption = "Nơi sinh";
                    ds.Tables[0].Columns["Candidate_IDCardNo"].Caption = "CMTND";
                    ds.Tables[0].Columns["Candidate_IDCardIssueAuthority"].Caption = "Nơi cấp";
                    ds.Tables[0].Columns["Candidate_IDCardIssueDate"].Caption = "Ngày cấp";
                    ds.Tables[0].Columns["Candidate_Address"].Caption = "Địa chỉ";
                    ds.Tables[0].Columns["Candidate_Phone"].Caption = "Điện thoại";
                    ds.Tables[0].Columns["Candidate_Date"].Caption = "Ngày nộp hồ sơ";
                    ds.Tables[0].Columns["Standard_Name"].Caption = "Trình độ";                   
                    ds.Tables[0].Columns["MaritalStatus_Name"].Caption = "Hôn nhân";                                                          
                    ds.Tables[0].Columns["Candidate_TransportMeans"].Caption = "Phương tiện đi lại";
                    ds.Tables[0].Columns["Candidate_WhenAvailable"].Caption = "Ngày có thể đi làm";
                    ds.Tables[0].Columns["Candidate_WorkOnShift"].Caption = "Đi làm ca";
                    ds.Tables[0].Columns["Candidate_MinSalaryExpected"].Caption = "Mức lương mong muốn";
                    ds.Tables[0].Columns["Candidate_Description"].Caption = "Ghi chú";
                    ReadExcelFile.exportToExcelForEmployeeList("DANH SÁCH ỨNG VIÊN", "", "", ds, Path.GetTempPath() + "\\" + _fileName);

                    try
                    {
                        System.Diagnostics.Process.Start(Path.GetTempPath() + "\\" + _fileName);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Không mở được File \n" + Path.GetTempPath() + "\\" + _fileName, "Thông báo");
                    }

                }
            }
            catch (Exception)
            {
                MessageBox.Show("File " + Path.GetTempPath() + "\\" + _fileName + " đang được mở bằng một ứng dụng khác.\nHãy chọn file khác!", "Thông báo");
            }
        }

     

            
       
    }
}

